package org.association.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.association.dao.AddressDao;
import org.association.entity.AddressEntity;
import org.association.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service("addressService")
public class AddressServiceImpl extends ServiceImpl<AddressDao, AddressEntity> implements AddressService {

    @Autowired
    private AddressDao addressDao;

    /**
     * 根据地址名单入库，如果名单表已存在数据，则不插入
     */
    @Override
    @Transactional
    public void initAddress(List<String> addresses) {
        Long count = addressDao.selectCount(new QueryWrapper<>());
        if(count != 0) {
            return ;
        }

        List<AddressEntity> insertAddress = new ArrayList<>();
        for(String address : addresses) {
            AddressEntity addressEntity = new AddressEntity();
            addressEntity.setContent(address);
            addressEntity.setUsed(0);
            insertAddress.add(addressEntity);
        }

        saveBatch(insertAddress);
    }

    @Override
    public String allocateAddress() {
        return addressDao.allocateAddress();
    }

    @Override
    public void allocate(String address) {
        addressDao.allocate(address);
    }


}